package org.idea.spring.look.up.factory;

import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;

import java.util.Map;

/**
 * @Author linhao
 * @Date created in 10:03 下午 2021/4/10
 */
class Text {
    int id;

    public Text(int id) {
        System.out.println("init");
        this.id = id;
    }

    public Text(){
        System.out.println("no arg init");
    }

    @Override
    public String toString() {
        return "Text{" +
                "id=" + id +
                '}';
    }
}

class A {
   void doJob(){
       System.out.println("do  Job  A");
   }
}
class B extends A {

    @Override
    void doJob() {
        System.out.println("do Job B");
    }
}
public class SpringListableLookUpDemo {

    @Bean(name = "text")
    public Text getText() {
        return new Text(1);
    }

    public static void main(String[] args) {
        AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext();
        annotationConfigApplicationContext.register(SpringListableLookUpDemo.class);
        annotationConfigApplicationContext.register(B.class);
        annotationConfigApplicationContext.register(A.class);
        annotationConfigApplicationContext.refresh();
        ListableBeanFactory listableBeanFactory = annotationConfigApplicationContext;
//        listableBeanFactory.getBeansWithAnnotation(Component.class);
//        Text text = (Text) listableBeanFactory.getBean("text");
        //如果查找的是一个父类，会顺便将其子类也查询出来
        String[] beanNameArr = listableBeanFactory.getBeanNamesForType(Text.class);
        for (String beanName : beanNameArr) {
            System.out.println(beanName);
        }
        Map<String, Text> map = listableBeanFactory.getBeansOfType(Text.class);
        System.out.println(map);


    }
}
